home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 6 / develop 6 code / TCP / NewsWatcher / NewsWatcher 2.0d15 source / source / sfutil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-27  |  6.6 KB  |  226 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------------
  2.  
  3.     sfutil.c
  4.  
  5.     This module contains Standard File utility routines.
  6.     
  7.     Portions copyright © 1990, Apple Computer.
  8.     Portions copyright © 1993, Northwestern University.
  9.  
  10. ----------------------------------------------------------------------------*/
  11.  
  12. #include <Packages.h>
  13. #include <stdio.h>
  14. #include <Finder.h>
  15. #include <Folders.h>
  16.  
  17. #include "glob.h"
  18. #include "sfutil.h"
  19. #include "dlgutil.h"
  20. #include "util.h"
  21.  
  22.  
  23.  
  24.  
  25. #define kDirSelectDlg            200        /* Directory selection standard file dialog */
  26. #define kSelectDirButtonTop     11
  27. #define kSelectDirButtonBottom    10
  28.  
  29. static FSSpec gDirTop;            /* file selected in top button */
  30. static FSSpec gDirBottom;        /* file selected in bottom button */
  31. static short gSelect;            /* 0 = cancel, 1 = top select button, 2 = bottom select button */
  32. static Str255 gPrevNameTop;        /* previous name in top button */
  33. static Str255 gPrevNameBottom;    /* previous name in bottom button */
  34.  
  35.  
  36.  
  37. /*----------------------------------------------------------------------------
  38.     MyStandardGetFile 
  39.     
  40.     Presents the StandardGetFile dialog.
  41.     
  42.     The parameters are the same as in StandardGetFile.
  43. ----------------------------------------------------------------------------*/
  44.  
  45. void MyStandardGetFile(FileFilterProcPtr fileFilter, short numTypes,
  46.     SFTypeList typeList, StandardFileReply *reply)
  47. {
  48.     PrepDialog();
  49.     StandardGetFile(fileFilter, numTypes, typeList, reply);
  50. }
  51.  
  52.  
  53.  
  54. /*----------------------------------------------------------------------------
  55.     MyStandardPutFile 
  56.     
  57.     Presents the StandardPutFile dialog.
  58.     
  59.     The parameters are the same as in StandardPutFile.
  60. ----------------------------------------------------------------------------*/
  61.  
  62. void MyStandardPutFile (const Str255 prompt, const Str255 defaultName,
  63.     StandardFileReply *reply)
  64. {
  65.     PrepDialog();
  66.     StandardPutFile(prompt, defaultName, reply);
  67. }
  68.  
  69.  
  70.  
  71. /*----------------------------------------------------------------------------
  72.     GetDirectoryFilter 
  73.     
  74.     Filter function for MyGetStandardDirectory.
  75. ----------------------------------------------------------------------------*/
  76.  
  77. static pascal Boolean GetDirectoryFilter (ParmBlkPtr pb, void *myDataPtr)
  78. {
  79.     return (pb->fileParam.ioFlAttrib & 0x10) == 0;
  80. }
  81.  
  82.  
  83.  
  84. /*----------------------------------------------------------------------------
  85.     GetDirectoryHook
  86.     
  87.     Dialog hook function for MyGetStandardDirectory.
  88. ----------------------------------------------------------------------------*/
  89.  
  90. static pascal short GetDirectoryHook (short item, DialogPtr dlg, void *myDataPtr)
  91. {
  92.     Handle itemHandleTop, itemHandleBottom;
  93.     short itemType;
  94.     Rect boxTop, boxBottom;
  95.     CInfoPBRec pb;
  96.     StandardFileReply *reply;
  97.     short width;
  98.     CStr255 buttonTitle;
  99.     Str255 name;
  100.     short foundVRefNum;
  101.     long foundDirID;
  102.  
  103.     if (GetWRefCon(dlg) != sfMainDialogRefCon) return item;
  104.     GetDItem(dlg, kSelectDirButtonTop, &itemType, &itemHandleTop, &boxTop);
  105.     GetDItem(dlg, kSelectDirButtonBottom, &itemType, &itemHandleBottom, &boxBottom);
  106.     
  107.     if (item == sfHookFirstCall) {
  108.         pb.dirInfo.ioNamePtr = gDirBottom.name;
  109.         pb.dirInfo.ioVRefNum = -SFSaveDisk;
  110.         pb.dirInfo.ioFDirIndex = -1;
  111.         pb.dirInfo.ioDrDirID = CurDirStore;
  112.         PBGetCatInfo(&pb, false);
  113.         gDirBottom.vRefNum = pb.dirInfo.ioVRefNum;
  114.         gDirBottom.parID = pb.dirInfo.ioDrParID;
  115.         gDirTop = gDirBottom;
  116.     } else {
  117.         reply = (StandardFileReply*)myDataPtr;
  118.         if (reply->sfIsFolder || reply->sfIsVolume) {
  119.             gDirBottom = reply->sfFile;
  120.             DlgEnableItem(dlg, kSelectDirButtonBottom, true);
  121.         } else {
  122.             DlgEnableItem(dlg, kSelectDirButtonBottom, false);
  123.             *gDirBottom.name = 0;
  124.         }
  125.         if (reply->sfFile.parID == fsRtParID) {
  126.             FindFolder(kOnSystemDisk, kDesktopFolderType, kCreateFolder,
  127.                 &foundVRefNum, &foundDirID);
  128.             pb.dirInfo.ioNamePtr = gDirTop.name;
  129.             pb.dirInfo.ioVRefNum = foundVRefNum;
  130.             pb.dirInfo.ioFDirIndex = -1;
  131.             pb.dirInfo.ioDrDirID = foundDirID;
  132.             PBGetCatInfo(&pb, false);
  133.             gDirTop.vRefNum = pb.dirInfo.ioVRefNum;
  134.             gDirTop.parID = pb.dirInfo.ioDrParID;
  135.         } else {
  136.             pb.dirInfo.ioNamePtr = gDirTop.name;
  137.             pb.dirInfo.ioVRefNum = reply->sfFile.vRefNum;
  138.             pb.dirInfo.ioFDirIndex = -1;
  139.             pb.dirInfo.ioDrDirID = reply->sfFile.parID;
  140.             PBGetCatInfo(&pb, false);
  141.             gDirTop.vRefNum = pb.dirInfo.ioVRefNum;
  142.             gDirTop.parID = pb.dirInfo.ioDrParID;
  143.         }
  144.     }
  145.     
  146.     if (!EqualString(gPrevNameBottom, gDirBottom.name, false, true)) {
  147.         pstrcpy(gPrevNameBottom, gDirBottom.name);
  148.         width = boxBottom.right - boxBottom.left - StringWidth("\pSelect “”") - 2*CharWidth(' ');
  149.         pstrcpy(name, gDirBottom.name);
  150.         TruncString(width, name, smTruncMiddle);
  151.         sprintf(buttonTitle, "Select “%#s”", name);
  152.         c2pstr(buttonTitle);
  153.         SetCTitle((ControlHandle)itemHandleBottom, (StringPtr)buttonTitle);
  154.         ValidRect(&boxBottom);
  155.     }
  156.     
  157.     if (!EqualString(gPrevNameTop, gDirTop.name, false, true)) {
  158.         pstrcpy(gPrevNameTop, gDirTop.name);
  159.         width = boxTop.right - boxTop.left - StringWidth("\pSelect “”") - 2*CharWidth(' ');
  160.         pstrcpy(name, gDirTop.name);
  161.         TruncString(width, name, smTruncMiddle);
  162.         sprintf(buttonTitle, "Select “%#s”", name);
  163.         c2pstr(buttonTitle);
  164.         SetCTitle((ControlHandle)itemHandleTop, (StringPtr)buttonTitle);
  165.         ValidRect(&boxTop);
  166.     }
  167.     
  168.     /* Handle clicks on Select and Cancel buttons. */
  169.     
  170.     if (item == kSelectDirButtonTop) {
  171.         gSelect = 1;
  172.         return sfItemCancelButton;
  173.     } else if (item == kSelectDirButtonBottom) {
  174.         gSelect = 2;
  175.         return sfItemCancelButton;
  176.     } else if (item == sfItemCancelButton) {
  177.         gSelect = 0;
  178.         return sfItemCancelButton;
  179.     }
  180.     
  181.     return item;
  182. }
  183.  
  184.  
  185.  
  186. /*----------------------------------------------------------------------------
  187.     MyStandardGetDirectory 
  188.     
  189.     Presents a custom standard get file dialog to select a directory.
  190.         
  191.     Exit:    reply->sfGood = true if directory selected, false if dialog canceled.    
  192.             reply->sfFile.vRefNum = vol ref num of selected directory.
  193.             reply->sfFile.parID = directory id of *parent* of selected directory.
  194.             reply->sfFile.name = name of selected directory.
  195.             *dirID = directory id of selected directory.
  196. ----------------------------------------------------------------------------*/
  197.  
  198. void MyStandardGetDirectory (StandardFileReply *reply, long *dirID)
  199. {
  200.     Point point = {-1,-1};
  201.     CInfoPBRec pb;
  202.     
  203.     PrepDialog();
  204.  
  205.     *gPrevNameTop = *gPrevNameBottom = 0;
  206.     
  207.     CustomGetFile(GetDirectoryFilter, -1, nil, reply, kDirSelectDlg, point,
  208.         GetDirectoryHook, nil, nil, nil, reply);
  209.         
  210.     reply->sfGood = gSelect != 0;
  211.     if (!reply->sfGood) return;
  212.     
  213.     if (gSelect == 1) {
  214.         reply->sfFile = gDirTop;
  215.     } else {
  216.         reply->sfFile = gDirBottom;
  217.     }
  218.  
  219.     pb.dirInfo.ioNamePtr = reply->sfFile.name;
  220.     pb.dirInfo.ioVRefNum = reply->sfFile.vRefNum;
  221.     pb.dirInfo.ioFDirIndex = 0;
  222.     pb.dirInfo.ioDrDirID = reply->sfFile.parID;
  223.     PBGetCatInfo(&pb, false);
  224.     *dirID = pb.dirInfo.ioDrDirID;       
  225. }
  226.